﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using ERPStudio;
using System.Data.SqlClient;

namespace ExBug
{
    public partial class ArticoleAliniateFrm : FrmTreeRezumat
    {
        /*private TreeNode RootNode = null;
        private TreeNode SelectedNode = null;*/

        public ArticoleAliniateFrm(ModuleClass mInfo)
            : base(mInfo)
        {
            InitializeComponent();
            localTree = articoleTV;
            workConnString = mInfo.workConnectionString;
            txtRoot = "Art./Alin.";
            ColoanaParinte = "AAParinte";
            HandleFillTree += new TreeLoadHandler(Frm_HandleFillTree);
            numeMacheta = Text;

            eSRPDataSet.ArticoleAliniate.TerminalColumn.DefaultValue = 1;
            eSRPDataSet.ArticoleAliniate.TitluColumn.DefaultValue = String.Empty;
            eSRPDataSet.ArticoleAliniate.CategoriiColumn.DefaultValue = "[CLT]";
            eSRPDataSet.ArticoleAliniate.TermDenumireColumn.DefaultValue = String.Empty;
            eSRPDataSet.ArticoleAliniate.StartDenumireColumn.DefaultValue = String.Empty;
            eSRPDataSet.ArticoleAliniate.Anexa6Column.DefaultValue = false;
            eSRPDataSet.ArticoleAliniate.CodUnitateColumn.DefaultValue = ModuleInfo.CodUnitate;
			eSRPDataSet.ArticoleAliniate.ExecutieCheltuieliColumn.DefaultValue = false;
        }

        void Frm_HandleFillTree(object sender, TreeViewEventArgs e)
        {
            this.articoleAliniateTableAdapter.Connection = DeschideConexiune(workConnString);

            if (e.Node.Level == 0)
            {
                this.articoleAliniateTableAdapter.FillBy(this.eSRPDataSet.ArticoleAliniate, ModuleInfo.CodUnitate, "N/A");
                eSRPDataSet.ArticoleAliniate.AAParinteColumn.DefaultValue = "N/A";
            }
            else
            {
                this.articoleAliniateTableAdapter.FillBy(this.eSRPDataSet.ArticoleAliniate, ModuleInfo.CodUnitate, e.Node.Text);
                eSRPDataSet.ArticoleAliniate.AAParinteColumn.DefaultValue = e.Node.Text.Trim();
            }
            InchideConexiune(articoleAliniateTableAdapter.Connection);
            articoleAliniateBindingSource.MoveFirst();
        }


        private void ArticoleAliniateFrm_Load(object sender, EventArgs e)
        {

            this.articoleAliniateTableAdapter.Connection = DeschideConexiune(workConnString);
            this.articoleAliniateTableAdapter.Fill(eSRPDataSet.ArticoleAliniate, ModuleInfo.CodUnitate);
            this.articoleTV.Nodes.Clear();
            TreeNode tnRoot = this.articoleTV.Nodes.Add(txtRoot);
            tnRoot.Tag =new NodeInfo("N/A");
            ColoanaPK = "ArtAl";
            ColoanaNume = "ArtAl";
            
            
            
            this.listBndSourceMain.Add(articoleAliniateBindingSource);
            this.listTableAdaptor.Add(articoleAliniateTableAdapter.Adapter);
            
            ///incarca articolele dupa setarea binding source
            LoadTree(tnRoot, articoleAliniateTableAdapter.Connection, ModuleInfo.CodUnitate.ToString());
            articoleAliniateBindingSource.Filter = String.Empty;
            if (articoleAliniateBindingSource.Count == 0)
            {
                try
                {
                    SqlCommand cmd = new SqlCommand("[exbug_GenereazaArticoleAlineate]", articoleAliniateTableAdapter.Connection);
                    cmd.CommandType = CommandType.StoredProcedure;
                    cmd.Parameters.Add(new SqlParameter("@CodUnitate", ModuleInfo.CodUnitate.ToString()));
                    cmd.ExecuteScalar();
					this.articoleAliniateTableAdapter.Fill(eSRPDataSet.ArticoleAliniate, ModuleInfo.CodUnitate);
                }
                catch (Exception ex)
                {
                    MessageBox.Show(ex.Message);
                    return;
                }
            }

            InchideConexiune(articoleAliniateTableAdapter.Connection);

            if (ModuleInfo.CurrentTheme == "tribunal")
            {
                AddButton("Sterge", global::ERPStudio.Properties.Resources.sterge, Sterge, ToolStripItemAlignment.Right, false);
            }
            AddButton("Analitice", global::ERPStudio.Properties.Resources.info, Analitice, ToolStripItemAlignment.Right, false);
            AddButton("Generare >>", global::ERPStudio.Properties.Resources.prezinta, Generare, ToolStripItemAlignment.Right, false);
            articoleAliniateDGV.Columns["denumireColumn"].Width = 200;
            articoleTV.SelectedNode = tnRoot;


        }

        public void Generare(object sender, EventArgs args)
        {
            AnaliticeConturiAAFrm frm = new AnaliticeConturiAAFrm(ModuleInfo,true);
            frm.ShowDialog();            
        }

        public void Analitice(object sender, EventArgs args)
        {
            if (articoleAliniateDGV.SelectedCells.Count == 0)
            {
                MessageBox.Show("Va rugam selectati un articol sau un aliniat!", "Atentie");
                return;
            }
            Int32 IndexRand = articoleAliniateDGV.SelectedCells[0].RowIndex;
            DataGridViewRow dr = articoleAliniateDGV.Rows[IndexRand];
            AnaliticeConturiAAFrm frm = new AnaliticeConturiAAFrm(ModuleInfo,false);
            if (Convert.ToBoolean(dr.Cells["terminalColumn"].Value) != true)
            {
                MessageBox.Show("Va rugam selectati un articol sau un aliniat fara elemente subordonate !", "Atentie");
                return;
            }
            frm.Articol = dr.Cells["ArtAlColumn"].Value.ToString();

            //frm.MdiParent = this.MdiParent;
            frm.ShowDialog();
        }

        override public void Salvare(object sender, EventArgs args)
        {
            MyTrace("Salvare");
            base.Salvare(this, args);
            if (EroareSalvare)
                return;
        }

               
    }
}
